const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    info: {},
    reapply: 0,
    promoter_shop_name: '',
    applyPromoterFlag: 0,
    memberCenterFlag: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.reapply) {
      let reapply = options.reapply;
      that.setData({
        reapply: reapply
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    app.restStatus(that, 'applyPromoterFlag');
    app.restStatus(that, 'memberCenterFlag');

    that.getApplyInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 分销商信息
   */
  getApplyInfo: function() {
    let that = this;
    app.sendRequest({
      url: 'System.Distribution.checkApplyPromoter',
      data: {},
      success: function(res) {

        console.log(res);
        if (res.code == 0) {
          let info = app.checkEmpty(res.data, {});
          info.promoter_info = app.checkEmpty(info.promoter_info, '');
          info.promoter_level = app.checkEmpty(info.promoter_level, []);

          // 已申请成功跳入分销中心
          if (info.promoter_info != '' && info.promoter_info.is_audit == 1) {
            wx.redirectTo({
              url: '/pagesnfx/pages/distribution/distributioncenter/distributioncenter',
            })
          }
          that.setData({
            info: info
          })
          that.loadUserConsume();
        }
      }
    })
  },

  /**
   * 用户消费
   */
  loadUserConsume: function() {
    let that = this;

    app.sendRequest({
      url: 'System.Distribution.userConsume',
      data: {},
      success: function(res) {

        if (res.code == 0) {
          let shop_sale_money = app.checkEmpty(res.data, 0);
          let info = that.data.info;
          let promoter_level = [];
          if (info != '') {
            for (let index in info.promoter_level) {
              if (parseFloat(info.promoter_level[index].level_money) <= parseFloat(shop_sale_money)) {
                promoter_level = info.promoter_level[index];
              }
            }
          }
          promoter_level = promoter_level.level_id == undefined ? '' : promoter_level;
          that.setData({
            shop_sale_money: shop_sale_money,
            promoter_level: promoter_level
          })
        }
      }
    });
  },

  /**
   * 输入店铺名称
   */
  inputPromoterShopName: function(e) {
    let that = this;
    let promoter_shop_name = e.detail.value;

    that.setData({
      promoter_shop_name: promoter_shop_name
    })
  },

  /**
   * 申请推广员
   */
  applyPromoter: function(e) {
    let that = this;
    let promoter_level = that.data.promoter_level;
    let shop_sale_money = that.data.shop_sale_money;
    let promoter_shop_name = that.data.promoter_shop_name;
    let applyPromoterFlag = that.data.applyPromoterFlag;

    if (applyPromoterFlag == 1) {
      return false;
    }
    app.clicked(that, 'applyPromoterFlag');

    if (parseFloat(promoter_level.level_money) > parseFloat(shop_sale_money)) {
      app.showBox(that, '您未满足消费条件！');
      app.restStatus(that, 'applyPromoterFlag');
      return false;
    }

    if (promoter_shop_name == '') {
      app.showBox(that, '请输入店铺名称！');
      app.restStatus(that, 'applyPromoterFlag');
      return false;
    }

    app.sendRequest({
      url: 'System.Distribution.applyPromoter',
      data: {
        shop_id: 0,
        promoter_shop_name: promoter_shop_name,
      },
      success: function(res) {

        if (res.code == 0) {
          if (res.data > 0) {
            app.showBox(that, '已提交申请,等待审核');
            that.setData({
              reapply: 0
            })
            that.getApplyInfo();
          } else {
            app.showBox(that, "申请失败");
            app.restStatus(that, 'applyPromoterFlag');
          }
        }
      }
    })
  },
})